package com.runfast.waimai.web.controller.notice;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.*;
import com.runfast.waimai.utils.TimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.*;

/**
 * ${DESCRIPTION}商家类型
 *
 * @author chenzheng
 * @create 2018-05-08 16:31
 **/
@RestController("noticeController")
@RequestMapping("/server/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private WaiMaiRunfastAgentbusinessService waiMaiRunfastAgentbusinessService;
    @Autowired
    private RunfastAgentApplicationService runfastAgentApplicationService;
    @Autowired
    private RunfastDriverApplicationService runfastDriverApplicationService;
    @Autowired
    private RunfastBusinessallianceService runfastBusinessallianceService;
    @Autowired
    private RunfastComplainService runfastComplainService;

    /*** 总公司通知列表带分页的*/
    @GetMapping("/list")
    public Result list(@PageableDefault Pageable pageable,Notice notice1) throws ParseException{
        //把代理商申请表里的信息转入通知信息表
        runfastAgentApplicationService.AgentApplicationToNotice();
        //把骑手申请表里的信息转入通知信息表
        runfastDriverApplicationService.DriverApplicationToNotice();
        //把商家申请表里的信息转入通知信息表
        runfastBusinessallianceService.BusinessallianceToNotice();
        //把客户投诉表里的信息转入通知信息表
        runfastComplainService.ComplainToNotice();
        Map<Object,Object> map1=new HashMap<Object, Object>();
        map1.put("pageable",pageable);
        if(notice1!=null){
            if(StringUtils.isNotBlank(notice1.getDate())){
                List<String> result = Arrays.asList(notice1.getDate().split(","));
                map1.put("startTime",TimeUtil.getDate(result.get(0)));
                map1.put("endTime",TimeUtil.getDate(result.get(1)));
            }
            if(StringUtils.isNotBlank(notice1.getTitle())){
                map1.put("title",notice1.getTitle());
            }
            if(notice1.getType()!=null){
                map1.put("type",notice1.getType());
            }
        }
        List<Notice> list=noticeService.selectByMapWithBLOBsWithPageable(map1);
        if(list.size()>0){
            for(Notice notice:list){
                if(notice.getAgentid()!=null){
                    WaiMaiRunfastAgentbusiness agentbusiness =waiMaiRunfastAgentbusinessService.setAgent(notice.getAgentid());
                    if(agentbusiness!=null){
                        notice.setAgentname(agentbusiness.getName());
                    }else{return Result.fail(null,"找不到对应的代理商！");}
                }
            }
        }
        Integer total=noticeService.countByMap(map1);
        Map<Object,Object> map=new HashMap<Object, Object>();
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
    }

    /*** 代理商通知列表带分页的*/
    @GetMapping("/agentList")
    public Result agentList(@PageableDefault Pageable pageable, Notice notice) throws ParseException {
        Map<Object,Object> map1=new HashMap<Object, Object>();
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        if(admin!=null && admin.getType()==2 && agentAccounts!=null){
            map1.put("agentId",agentAccounts.getAgentid());
        }else if(admin!=null && admin.getType()==1){
            map1.put("onlyadmin",true);
        }
        if(notice!=null){
            if(notice.getSelectType()!=null){
                map1.put("selectType",notice.getSelectType());
            }
            if(StringUtils.isNotBlank(notice.getDate())){
                List<String> result = Arrays.asList(notice.getDate().split(","));
                map1.put("startTime",TimeUtil.getDate(result.get(0)));
                map1.put("endTime",TimeUtil.getDate(result.get(1)));
            }
        }
        map1.put("pageable",pageable);
        List<Notice> list=noticeService.selectByMapWithBLOBsWithPageable(map1);
        if(list.size()>0){
           for(Notice notice1:list){
               if(admin!=null && admin.getType()==2 && agentAccounts!=null){
                   if(notice1.getAgentid()==null && StringUtils.isNotBlank(notice1.getAgentids())) {
                       List<String> list1 = Arrays.asList(notice1.getAgentids().split(","));
                       if (list1.contains(agentAccounts.getAgentid().toString())) {
                           notice1.setHaveread(true);
                       }
                   }else if(notice1.getAgentid()==null && !StringUtils.isNotBlank(notice1.getAgentids())){
                       notice1.setHaveread(false);
                   }
               }
              /* else if(admin!=null && admin.getType()==1){

               }*/
           }
        }
        Integer total=noticeService.countByMap(map1);
        Map<Object,Object> map=new HashMap<Object, Object>();
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
    }

    /*** 代理商查看通知信息*/
    @GetMapping("/info")
    public Result info(Integer id){
        //Map<Object,Object> map1=new HashMap<Object, Object>();
        Notice notice=new Notice();
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        if(admin!=null && admin.getType()==2 && agentAccounts!=null){
            //map1.put("agentId",agentAccounts.getAgentid());
            if(id!=null){
                notice=noticeService.selectByPrimaryKey(id);
                if(notice.getAgentid()!=null){
                    WaiMaiRunfastAgentbusiness agentbusiness =waiMaiRunfastAgentbusinessService.setAgent(notice.getAgentid());
                    if(agentbusiness!=null){
                        notice.setAgentname(agentbusiness.getName());
                    }else{return Result.fail(null,"找不到对应的代理商！");}
                }
                if(notice.getAgentid()!=null && notice.getHaveread()==false){
                    notice.setHaveread(true);
                    noticeService.updateByPrimaryKeySelective(notice);
                }else if(notice.getAgentid()==null && StringUtils.isNotBlank(notice.getAgentids())) {
                    List<String> list =new ArrayList<String>();
                    list = Arrays.asList(notice.getAgentids());
                    if (!list.contains(agentAccounts.getAgentid().toString())) {
                        String agentIds=notice.getAgentids();
                        agentIds=agentIds+","+agentAccounts.getAgentid().toString();
                        notice.setAgentids(agentIds);
                        noticeService.updateByPrimaryKeySelective(notice);
                    }
                }else if(notice.getAgentid()==null && !StringUtils.isNotBlank(notice.getAgentids())){
                    notice.setAgentids(agentAccounts.getAgentid().toString());
                    noticeService.updateByPrimaryKeySelective(notice);
                }
        }else{return Result.fail(null,"传入参数不对！");}
        }else if(admin!=null && admin.getType()==1){
            if(id!=null){
                notice=noticeService.selectByPrimaryKey(id);
                notice.setHaveread(true);
                noticeService.updateByPrimaryKeySelective(notice);
            }else{return Result.fail(null,"传入参数不对！");}
        }
        return Result.ok(null,notice);
    }

    /*** 保存通知*/
    @PostMapping("/save")
    public Result save(@RequestBody Notice notice){
        if(notice!=null && StringUtils.isNotBlank(notice.getTitle()) && StringUtils.isNotBlank(notice.getContent()) ){
            notice.setCreatetime(new Date());
            notice.setDeleted(false);
            notice.setHaveread(false);
            noticeService.insert(notice);
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }
    /*** 删除通知*/
    @GetMapping("/delete")
    public Result delete(Integer id){
        if(id!=null){
            noticeService.deleteByPrimaryKey(id);
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }



    /**代理商未读通知条数*/
    @GetMapping("/agentUnreadNum")
    public Result agentUnreadNum(){
        Map<Object,Object> map1=new HashMap<Object, Object>();
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        if(admin!=null &&  admin.getType()==2 &&agentAccounts!=null){
            map1.put("agentId",agentAccounts.getAgentid());
        }else if(admin!=null && admin.getType()==1){
            map1.put("onlyadmin",true);
        }
        map1.put("selectType",2);
        Integer total=noticeService.countByMap(map1);
        return Result.ok(null,total);
    }








    /**
     * 通知列表
     */
   /* @GetMapping("/select")
    public Result select(){

        RunfastBusinessTypeExample example=new RunfastBusinessTypeExample();
        example.createCriteria().andParentIdEqualTo(0);
        List<RunfastBusinessType> list = runfastBusinessTypeService.selectByExample(example);
        if(list.size()>0){
            for(RunfastBusinessType type:list){
                RunfastBusinessTypeExample example1=new RunfastBusinessTypeExample();
                example1.createCriteria().andParentIdEqualTo(type.getId());
                List<RunfastBusinessType> list1=runfastBusinessTypeService.selectByExample(example1);
                type.setSubList(list1);
            }
        }
        return Result.ok(null,list);
    }*/


    /**
     * 修改通知
     */
    /*@PostMapping("/update")
    public Result update(@RequestBody RunfastBusinessType type){
        if(type==null){return Result.fail(null,"品类信息不能为空！");}
        if(type.getSubType().size()>0){
            RunfastBusinessTypeExample example=new RunfastBusinessTypeExample();
            example.createCriteria().andParentIdEqualTo(type.getId());
            List<RunfastBusinessType> subList=runfastBusinessTypeService.selectByExample(example);
            List<Integer> list=new ArrayList<Integer>();
            if(subList.size()>0){
                for(RunfastBusinessType type1:subList){
                    list.add(type1.getId());
                }
            }
            for(Map<String, String> sub:type.getSubType()){
                     System.out.println(sub.get(0));
                     if(StringUtils.isNotBlank(sub.get("id"))){
                         if(list.contains(StringUtil.stringToInteger( sub.get("id")))){
                             RunfastBusinessType businessType=new RunfastBusinessType();
                             businessType.setId(StringUtil.stringToInteger( sub.get("id")));
                             businessType.setName(sub.get("name"));
                             runfastBusinessTypeService.updateByPrimaryKeySelective(businessType);
                         }
                         list.remove(StringUtil.stringToInteger( sub.get("id")));
                     }else{
                         RunfastBusinessType businessType=new RunfastBusinessType();
                         businessType.setParentId(type.getId());
                         businessType.setName(sub.get("name"));
                         businessType.setCreatetime(new Date());
                         runfastBusinessTypeService.insertSelective(businessType);
                     }
            }
            if(list.size()>0){
                for(Integer l:list){
                    RunfastBusinessExample example1=new RunfastBusinessExample();
                    example1.createCriteria().andMainClassIdEqualTo(type.getId());
                    example1.or().andSubClassId1EqualTo(type.getId());
                    example1.or().andSubClassId2EqualTo(type.getId());
                    Integer i=runfastBusinessService.countByExample(example1);
                    if(i>0){
                        return Result.fail(null,"删除的品类已被应用，不能删除！");
                    }else{
                        runfastBusinessTypeService.deleteByPrimaryKey(l);
                    }
                }
            }
        }
        type.setName(type.getBroadHeading());
        runfastBusinessTypeService.updateByPrimaryKeySelective(type);
        return Result.ok(null);
    }*/
}
